package com.example.wxordersystem.web;

import com.example.wxordersystem.entity.ProductCategory;
import com.example.wxordersystem.entity.ProductInfo;
import com.example.wxordersystem.exception.SellException;
import com.example.wxordersystem.form.ProductForm;
import com.example.wxordersystem.service.CategoryService;
import com.example.wxordersystem.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author 常红伟
 */

@RestController
@RequestMapping("/seller/product")
@Slf4j
public class SellProductController {

    @Autowired
    ProductService productService;

    @Autowired
    CategoryService categoryService;

    /**
     * 卖家展示商品列表
     * @param page
     * @param size
     * @param map
     * @return
     */
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page" , defaultValue = "1") Integer page,
                             @RequestParam(value = "size" , defaultValue = "10")Integer size,
                             Map<String,Object> map){

        List<ProductInfo> productInfoList=productService.findAll();
        map.put("productInfoList",productInfoList);
        map.put("page",page);
        map.put("size",size);
        return new ModelAndView("product/list",map);
    }


    /**
     * 卖家将一个商品进行上架
     * @param productId
     * @param map
     * @return
     */
    @RequestMapping("/on_sale")
    public ModelAndView onSale(@RequestParam(value = "productId") String productId,
                               Map<String,Object> map){

        try {
            productService.onSale(productId);
        }catch (SellException e){
            map.put("msg",e.getMessage());
            map.put("url","list");
            return new ModelAndView("common/error",map);
        }

        map.put("url","list");
        return new ModelAndView("common/success",map);
    }

    /**
     * 卖家将商品下架
     * @param productId
     * @return
     */
    @RequestMapping("/off_sale")
    public ModelAndView offSale(@RequestParam(value = "productId") String productId,
                                Map<String,Object>map){
        try {
            productService.offSale(productId);
        }catch (SellException e){
            map.put("msg",e.getMessage());
            map.put("url","list");
            return new ModelAndView("common/error",map);
        }
        map.put("url","list");
        return new ModelAndView("common/success",map);
    }

    /**
     * 商家保存和更新商品
     * @param productForm
     * @param bindingResult
     * @param map
     * @return
     */
    @PostMapping("/save")
    public ModelAndView save(@Valid ProductForm productForm,
                             BindingResult bindingResult,
                             Map<String,Object>map){

        if (bindingResult.hasErrors()){
            map.put("msg",bindingResult.getFieldError().getDefaultMessage());
            map.put("url","index");
            return new ModelAndView("common/error",map);
        }

        ProductInfo productInfo = new ProductInfo();
        try {
             if(productForm.getProductId() == null || productForm.getProductId()==""){
                BeanUtils.copyProperties(productForm,productInfo);
                productService.save(productInfo);
            }else {
                productInfo=productService.findOne(productForm.getProductId());
                BeanUtils.copyProperties(productForm,productInfo);
                productService.update(productInfo);
            }
        }catch (SellException e){
            map.put("msg",e.getMessage());
            map.put("url","index");
            return new ModelAndView("common/error",map);
        }

        map.put("url","list");
        return new ModelAndView("common/success",map);

    }

    /**
     * 列表
     * @param productId
     * @param map
     * @return
     */
    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "productId", required = false) String productId,
                              Map<String, Object> map) {
        if (!StringUtils.isEmpty(productId)) {
            ProductInfo productInfo = productService.findOne(productId);
            map.put("productInfo", productInfo);
        }

        //查询所有的类目
        List<ProductCategory> categoryList = categoryService.findAll();
        map.put("categoryList", categoryList);

        return new ModelAndView("product/index", map);
    }





}
